package com.hotelsystem.reservations.layouts;

import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.hotelsystem.reservation.controllers.RoomListController;
import com.hotelsystem.reservation.controllers.SaveReservationController;
import com.hotelsystem.reservations.persons.Client;
import com.hotelsystem.reservations.persons.GenderEnum;
import com.hotelsystem.reservations.reservations.ReservationManagement;
import com.hotelsystem.reservations.rooms.Room;
import com.hotelsystem.reservations.rooms.RoomManagement;

public class CreateReservationView {
	private JPanel mainPanel;
	private JButton btnSave, btnCancel, btnOpenClientList, btnOpenRoomList, btnRoomDetails;
	private JLabel lblStartDate, lblEndDate, lblClientFullName, lblRoomNumber;
	private JTextField txtStartDate, txtEndDate, txtClientFullName, txtRoomNumber;
	private JList<String> reservationClients;
	private ReservationManagement reservationManagement;
	private RoomListView rlv;
	private RoomListController roomListControler;
	private int roomIndex;
	private RoomManagement roomManagement;
	private Room room;
	private JFrame clientFrame;
	private UserListView userListView;

	
	
	
	public Room getRoom() {
		return room;
	}

	public void setRoom(Room room) {
		this.room = room;
	}

	public JTextField getTxtStartDate() {
		return txtStartDate;
	}

	public void setTxtStartDate(JTextField txtStartDate) {
		this.txtStartDate = txtStartDate;
	}

	public JTextField getTxtEndDate() {
		return txtEndDate;
	}

	public void setTxtEndDate(JTextField txtEndDate) {
		this.txtEndDate = txtEndDate;
	}

	public JPanel getMainPanel() {
		return mainPanel;
	}
	
	public JList<String> getReservationClients() {
		return reservationClients;
	}
	
	public CreateReservationView() {
		super();
		reservationManagement = new ReservationManagement();
		buildUI();
		addListeners();
	}

	private void addListeners() {
		// TODO Auto-generated method stub

	}

	/**
	 * Initialize all components in the mainPanel
	 */
	private void initializeComponents() {
		mainPanel = new JPanel();
		mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
		mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		btnSave = new JButton("Save");
		btnCancel = new JButton("Cancel");
		btnOpenClientList = new JButton("Pick client");
		btnOpenRoomList = new JButton("Pick room");
		btnRoomDetails = new JButton("Show details");
		lblStartDate = new JLabel("Reservation start date:");
		lblEndDate = new JLabel("Reservation end date:");
		lblClientFullName = new JLabel("Reservation clients:");
		lblRoomNumber = new JLabel("Room number:");
		txtStartDate = new JTextField(12);
		txtEndDate = new JTextField(12);
		txtClientFullName = new JTextField(20);
		txtRoomNumber = new JTextField(3);
		reservationClients = new JList<String>();	
		roomListControler = new RoomListController();
		roomManagement = new RoomManagement();
		
	}
	
	/**
	 * Builds the mainPanel object
	 */
	private void buildUI() {
		initializeComponents();
		
		JPanel innerPanel = new JPanel(new GridBagLayout());
		
		// StartDate
		GridBagConstraints gbc = createGbc(0, 0);
		innerPanel.add(lblStartDate, gbc);
		gbc = createGbc(1, 0);
		innerPanel.add(txtStartDate, gbc);
	
		// EndDate
		gbc = createGbc(0, 1);
		innerPanel.add(lblEndDate, gbc);
		gbc = createGbc(1, 1);
		innerPanel.add(txtEndDate, gbc);
		
		// Client
		gbc = createGbc(0, 2);
		innerPanel.add(lblClientFullName, gbc);
		gbc = createGbc(1, 2);
		JPanel clientComponents = new JPanel(new FlowLayout());
		clientComponents.add(reservationClients);
		clientComponents.add(btnOpenClientList);
		innerPanel.add(clientComponents, gbc);		
		
		// Room 
		gbc = createGbc(0, 3);
		innerPanel.add(lblRoomNumber, gbc);	
		gbc = createGbc(1, 3);
		JPanel roomComponents = new JPanel(new FlowLayout());
		roomComponents.add(txtRoomNumber);
		roomComponents.add(btnRoomDetails);
		roomComponents.add(btnOpenRoomList);	
		innerPanel.add(roomComponents, gbc);	
			
		// Nav buttons
		JPanel navButtonsPanel = new JPanel(new FlowLayout());
		
		btnSave.addActionListener(new SaveReservationController(this));
		
		btnOpenClientList.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				clientFrame = new JFrame();
				userListView = new UserListView();
				clientFrame.add(userListView.getMainPanel());
				
				
				clientFrame.setTitle("Clients");
				clientFrame.setSize(400, 200);
				clientFrame.setVisible(true);
				
			}
		});
		
		
		btnOpenRoomList.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				rlv = new RoomListView();
				
				
				final JFrame roomFrame = new JFrame();
				roomFrame.add(rlv.getMainPanel());
				roomListControler.setRoomListView(rlv);
				
				roomListControler.fillList();
				
				rlv.addListenerForOpen(new ActionListener(){

					@Override
					public void actionPerformed(ActionEvent e) {
						
						roomIndex=rlv.getRoomsList().getSelectedIndex();
						room = roomManagement.getRooms().get(roomIndex);
						txtRoomNumber.setText(room.getRoomNumber() + "");
						roomFrame.setVisible(false);
					}
					
				});
				
				roomFrame.setTitle("Rooms");
				roomFrame.setSize(400, 200);
				roomFrame.setVisible(true);
				
			}
			
			
		});
		
		navButtonsPanel.add(btnSave);
		
		navButtonsPanel.add(btnCancel);
		
		mainPanel.add(innerPanel);
		mainPanel.add(navButtonsPanel);
	}
	
	/**
	 * 
	 * @param x column x
	 * @param y column y
	 * @return GridBagConstraints object
	 */
	private GridBagConstraints createGbc(int x, int y)
	{
		  GridBagConstraints gbc = new GridBagConstraints();
	      gbc.gridx = x;
	      gbc.gridy = y;
	      gbc.gridwidth = 1;
	      gbc.gridheight = 1;

	      gbc.anchor = (x == 0) ? GridBagConstraints.EAST : GridBagConstraints.WEST;
	      gbc.fill =  GridBagConstraints.NONE;

	      gbc.insets = new Insets(2, 2, 0, 2);
	//      gbc.weightx = (x == 0) ? 0.1 : 1.0;
	//      gbc.weighty = 1.0;
	      return gbc;
	}
	
	public static void main(String[] args) {
		CreateReservationView layout = new CreateReservationView();
		JFrame frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JPanel panel = layout.getMainPanel();
		frame.add(panel);
		frame.setSize(500, 300);
		frame.setTitle("Reservations");
		
		JList<String> clientsList = layout.getReservationClients();
		DefaultListModel<String> clientModels = new DefaultListModel<>();
		
//		Client client1 = new Client("John", "Doe", "777", "1234", GenderEnum.MALE, 23, "john.doe@mail.bg", "Bulgarian",
//				null, null);
//		Client client2 = new Client("Donny", "Brasco", "888", "0000", GenderEnum.MALE, 40, "donny.brasco@mail.bg", "Sicilian",
//				null, null);
//		Client client3 = new Client("Some", "Longman", "888", "0000", GenderEnum.MALE, 40, "donny.brasco@mail.bg", "Sicilian",
//				null, null);
//		
//		ArrayList<Client> clients = new ArrayList<Client>();
//		clients.add(client1);
//		clients.add(client2);
//		clients.add(client3);
		
//		for (Client client : clients) {
//			clientModels.addElement(client.getFirstName() + " "
//					+ client.getLastName());
//		}
		clientsList.setModel(clientModels);
			
		frame.setVisible(true);
	}

}
